package cn.jiyun.controller;


import cn.jiyun.pojo.Dto;
import cn.jiyun.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("users")
public class DtoController {

    @Autowired
    private Service service;

    @RequestMapping("/{page}")
    public String showpage(@PathVariable  String page){
        return page;
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<Dto> list=service.list();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("/dtoadd")
    public String dtoadd(Dto dto){
        service.dtoadd(dto);
        return "redirect:/users/list";
    }


    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("/login")
    public String login(Model model, Dto dto) {
        Dto dto1 = service.login(dto);
        if(dto1!=null){
            model.addAttribute("user", dto1);
            return "redirect:/users/list";
        }
        return "login";
    }

}
